﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using System;
using System.Threading.Tasks;
using WebApi.Helpers;
using WebApi.Model;

namespace WebApi.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class PutOnRecordController : ControllerBase
    {
        private readonly ILogger<PutOnRecordController> logger;
        public PutOnRecordController(ILogger<PutOnRecordController> logger)
        {
            this.logger = logger;
        }

        /// <summary>
        /// 获取备案信息
        /// </summary>
        /// <param name="yNumber">备案号</param>
        /// <returns></returns>
        [HttpGet("{yNumber}")]
        public async Task<ReturnModel> Get(string yNumber)
        {
            try
            {
                logger.LogInformation($"{nameof(PutOnRecordController)}/{nameof(Get)}:{yNumber}");
                ReturnModel returnModel = new ReturnModel();
                returnModel.Data = await DbHelper.PutOnRecord.GetFirstAsync(it => it.YNumber == yNumber);
                returnModel.Count = returnModel.Data == null ? 0 : 1;
                return returnModel;
            }
            catch (Exception ex)
            {
                return logger.ErrorModel(nameof(PutOnRecordController) + "-" + nameof(Get), ex);
            }
        }
    }
}
